import request from "../utils/request";

export async function exportExcel(type, params = {}) {
    console.log(type);
    return request.get({
      url: `/museum/${type}`,
      method: 'get',
      responseType: 'blob',
      params: params
    }).then(response => {
      // 从响应头解析文件名
      const contentDisposition = response.headers['content-disposition'];
      const fileName = decodeURIComponent(
        contentDisposition.split('filename*=utf-8\'\'')[1]
      ).replace(/"/g, '');
  
      // 创建下载链接
      const blob = new Blob([response.data], { 
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
      });
      const link = document.createElement('a');
      link.href = URL.createObjectURL(blob);
      link.download = fileName;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      URL.revokeObjectURL(link.href);
    }).catch(error => {
      // 增强错误处理
      if (error.response?.status === 400) {
           console.log('请求参数错误');
      } else {
       console.log('导出失败');
      }
      throw error;
    });
  }